package com.jams.goodox.activity.mine.settings;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hangzhou.jin.customview.LoadView;
import com.hangzhou.jin.customview.SpaceItemDecoration;
import com.jams.goodox.BaseActivity;
import com.jams.goodox.R;
import com.jams.goodox.SePref;
import com.jams.goodox.adapter.IndexSuggestListAdapter;
import com.jams.goodox.constant.Constant;
import com.jams.goodox.entity.PageInfo;
import com.jams.goodox.entity.Suggestion;
import com.jams.goodox.net.RetrofitCallbackListener;
import com.jams.goodox.net.services.MineService;
import com.jams.goodox.utils.StyleUtil;
import com.jams.goodox.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import retrofit2.Call;

/**
 * Created by Jin on 2016/6/4.
 * Description 我的建议列表
 */
public class IndexSuggestList extends BaseActivity {

	@Bind(R.id.recyclerView)
    RecyclerView recyclerView;
	@Bind(R.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;
	@Bind(R.id.loadView)
    LoadView loadView;

	private Activity activity;

	private IndexSuggestListAdapter mAdapter;

	private static final int PAGE_SIZE = 10;
	private int curPage = 1;

	private String suggestType = "1";
	int all = 0;
	private List<Suggestion.DataBean.ResultListBean> listBeen;

	@Override
	protected int getLayoutId() {
		return R.layout.mine_suggest_list;
	}

	@Override
	protected void init() {
		activity = this;

		suggestType = getIntent().getStringExtra("suggestType");
		String title = getIntent().getStringExtra("title");
		StyleUtil.init(this, title);

		if (suggestType.equalsIgnoreCase("1"))
			all = 0;
		else if (suggestType.equalsIgnoreCase("0"))
			all = 1;
		listBeen=new ArrayList<>();
		recyclerView.setLayoutManager(new LinearLayoutManager(activity));
		mAdapter = new IndexSuggestListAdapter(listBeen);
		recyclerView.setAdapter(mAdapter);
		// 设置8dp的间隙
		int spacingInPixels = getResources().getDimensionPixelSize(R.dimen.margin_size_small);
		recyclerView.addItemDecoration(new SpaceItemDecoration(spacingInPixels));
		mAdapter.openLoadMore(PAGE_SIZE, true);

		// 自定义上拉View
		mAdapter.setLoadingView(getLoadMoreBootView(activity));
		mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
			@Override
			public void onLoadMoreRequested() {
				loadData(true);
			}
		});

		mAdapter.setOnRecyclerViewItemClickListener(new BaseQuickAdapter.OnRecyclerViewItemClickListener() {
			@Override
			public void onItemClick(View view, int i) {
				Bundle b = new Bundle();
				b.putString("id", ((List<Suggestion.DataBean.ResultListBean>) mAdapter.getData()).get(i).getSugId());
				qStartActivity(IndexSuggestListDetail.class, b);
			}
		});

        loadView.loading();
		curPage = 1;
		loadData(false);
		swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
			@Override
			public void onRefresh() {
				curPage = 1;
				loadData(false);
			}
		});
	}

	// 加载第一页的数据
	private void loadData(boolean isLoadMore) {
		Map<String, String> params = Constant.getPagerParams(isLoadMore?curPage+1:curPage);
		params.put("type", "1");
		if (( all + "").equalsIgnoreCase("0"))
			params.put("communityId", SePref.getCommunityId());
		if (suggestType.equalsIgnoreCase("0"))
			suggestType = "platform";
//		else
//			suggestType = "house";
		enqueue(createCallService(MineService.class).getSuggestion(suggestType,params), new RetrofitCallbackListener<Suggestion>() {
			@Override
			public void onSuccess(Call<Suggestion> call, Suggestion result) {
				swipeLayout.setRefreshing(false);
				loadView.loadComplete();
				if(result.getData()==null||result.getData().getResultList()==null||result.getData().getPageInfo()==null)return;
				List<Suggestion.DataBean.ResultListBean> list = result.getData().getResultList();
				PageInfo pageInfo=result.getData().getPageInfo();
				curPage=pageInfo.getCurPage();
				if(curPage==1)listBeen.clear();
				listBeen.addAll( result.getData().getResultList());
				mAdapter.notifyDataChangedAfterLoadMore(pageInfo.isHasMore());
				if (list.size() == 0) {
					loadView.loadCompleteNoData(R.mipmap.holder_no_common, R.string.suggest_no);
					return;
				}
			}

			@Override
			public void onError(String msg) {
				// 无网络情况下 结束加载，添加提示
				if (null != loadView && null != swipeLayout) {
					loadView.loadComplete();
					swipeLayout.setRefreshing(false);
					ToastUtil.show(msg);
				}
			}
		});
	}

	@Override
	public View getLoadMoreBootView(Context c) {
		View view = LayoutInflater.from(c).inflate(R.layout.load_more_footers, null);
		view.setLayoutParams(new DrawerLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
		return view;
	}
}
